package com.cheyunkeji.er.adapter.fastevaluate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.fast_evaluate.CarModel;
import com.cheyunkeji.er.bean.fast_evaluate.CarModelTitle;

import java.util.ArrayList;

/**
 * Created by Michael on 2017/4/24 18:25.
 */

public class CarModelLvAdapter extends MyBaseAdapter {

    //CarModelTitle类的type标志
    private static final int TYPE_A = 0;
    //CarModel类的type标志
    private static final int TYPE_B = 1;

    public CarModelLvAdapter(ArrayList<Object> dataList, Context context) {
        super(dataList, context);
    }

    public int getItemViewType(int position) {
        int result = 0;
        if (dataList.get(position) instanceof CarModelTitle) {
            result = TYPE_A;
        } else if (dataList.get(position) instanceof CarModel) {
            result = TYPE_B;
        }
        return result;
    }

    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;

        int type = getItemViewType(position);

        if (convertView == null) {

            holder1 = new ViewHolder1();
            holder2 = new ViewHolder2();

            switch (type) {

                case TYPE_A:

                    convertView = LayoutInflater.from(context).inflate(R.layout.er_layout_title, null);
                    holder1.titleDesc = (TextView) convertView.findViewById(R.id.tv_title_desc);
                    convertView.setTag(R.id.tag_title, holder1);
                    break;
                case TYPE_B:

                    convertView = LayoutInflater.from(context).inflate(R.layout.er_layout_content, null);
                    holder2.contentDesc = (TextView) convertView.findViewById(R.id.tv_content_desc);
                    convertView.setTag(R.id.tag_content, holder2);

                    break;

                default:
                    break;
            }

        } else {

            switch (type) {

                case TYPE_A:
                    holder1 = (ViewHolder1) convertView.getTag(R.id.tag_title);
                    break;
                case TYPE_B:
                    holder2 = (ViewHolder2) convertView.getTag(R.id.tag_content);
                    break;

                default:
                    break;
            }
        }

        Object obj = dataList.get(position);

        switch (type) {
            case TYPE_A:
                CarModelTitle title = (CarModelTitle) obj;
                holder1.titleDesc.setText(title.getTitleName());
                break;
            case TYPE_B:
                CarModel carInfo = (CarModel) obj;
                holder2.contentDesc.setText(carInfo.getName());
                break;

            default:
                break;
        }


        return convertView;
    }

    static class ViewHolder1 {

        TextView titleDesc;
    }

    static class ViewHolder2 {

        TextView contentDesc;
    }

}
